package com.bocloud.cmp.web.controller.resource;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.bocloud.cmp.web.controller.BasicController;
import com.bocloud.common.enums.BoCloudMethod;
import com.bocloud.common.enums.BoCloudService;
import com.bocloud.common.model.BsmResult;
import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.JSONTools;
import com.bocloud.common.utils.MapTools;
import com.bocloud.common.utils.ResultTools;
import com.bocloud.registry.http.core.ServiceFactory;
import com.bocloud.registry.http.model.RemoteService;
import com.bocloud.registry.utils.UrlTranslator;

/**
 * 机柜相关接口
 * 
 * @author dmw
 *
 */
@RestController
@RequestMapping("/rack")
public class RackController {
	private static final String BASE_SERVICE = "/rack";
	private static final BoCloudService SERVICE = BoCloudService.Cmp;
	@Autowired
	private ServiceFactory serviceFactory;
	@Autowired
	private BasicController basicController;

	/**
	 * 获取机柜列表
	 * 
	 * @param page
	 *            当前页码
	 * @param rows
	 *            页面数据大小
	 * @param params
	 *            查询参数，例如：[{"param":{"name":"aaa","password":"1245"},"sign":
	 *            "EQ|UEQ"},{"param":{"name":"aaa","password":"1245"},"sign":
	 *            "EQ|UEQ"}]
	 * @param sorter
	 *            排序参数，例如：{"name":0|1,"password":0|1},0表示增序，1表示降序
	 * @param simple
	 *            简单查询标记，只有true和false,为false时返回机柜的详细信息，为true时只返回id和name值。
	 * @return 数据结果集
	 */
	@RequestMapping(value = "/list", method = { RequestMethod.POST })
	public BsmResult list(@RequestParam(value = Common.PAGE, required = false, defaultValue = Common.ONE) Integer page,
			@RequestParam(value = Common.ROWS, required = false, defaultValue = Common.TEN) Integer rows,
			@RequestParam(value = Common.PARAMS, required = false) String params,
			@RequestParam(value = Common.SORTER, required = false) String sorter,
			@RequestParam(value = Common.SIMPLE, required = false) boolean simple, HttpServletRequest request) {
		return basicController.list(page, rows, params, sorter, simple, BASE_SERVICE, request,
				RackController.class.getSimpleName());
	}

	/**
	 * 添加机柜
	 * 
	 * @param params
	 *            机柜属性信息的json字符串，在数据转换时会发生异常，对外抛出400异常【Bad Request】
	 * @return 添加结果
	 */
	@RequestMapping(value = "/create", method = { RequestMethod.POST })
	public BsmResult create(@RequestParam(value = Common.PARAMS, required = true) String params,
			HttpServletRequest request) {
		return basicController.create(params, BASE_SERVICE, request, RackController.class.getSimpleName());
	}

	/**
	 * 修改机柜
	 * 
	 * @param params
	 *            机柜属性信息的json字符串，在数据转换时会发生异常，对外抛出400异常【Bad Request】
	 * @return
	 */
	@RequestMapping(value = "/modify", method = { RequestMethod.POST })
	public BsmResult modify(@RequestParam(value = Common.PARAMS, required = true) String params,
			HttpServletRequest request) {
		return basicController.modify(params, BASE_SERVICE, request, RackController.class.getSimpleName());
	}

	/**
	 * 移除机柜
	 * 
	 * @param params
	 *            机柜属性信息的json字符串，在数据转换时会发生异常，对外抛出400异常【Bad Request】
	 * @return 操作结果
	 */
	@RequestMapping(value = "/remove", method = { RequestMethod.POST })
	public BsmResult remove(@RequestParam(value = Common.PARAMS, required = true) String params,
			HttpServletRequest request) {
		return basicController.remove(params, BASE_SERVICE, request, RackController.class.getSimpleName());
	}

	/**
	 * 查看机柜详细信息
	 * 
	 * @param params
	 *            机柜属性信息的json字符串，在数据转换时会发生异常，对外抛出400异常【Bad Request】
	 * @return 操作结果
	 */
	@RequestMapping(value = "/detail", method = { RequestMethod.GET })
	public BsmResult detail(@RequestParam(value = Common.PARAMS, required = true) String params,
			HttpServletRequest request) {
		return basicController.detail(params, BASE_SERVICE, request, RackController.class.getSimpleName());
	}

	/**
	 * 查看机柜基本信息
	 * 
	 * @param params
	 *            机柜属性信息的json字符串，在数据转换时会发生异常，对外抛出400异常【Bad Request】
	 * @return 操作结果
	 */
	@RequestMapping(value = "/basic", method = { RequestMethod.GET })
	public BsmResult basic(@RequestParam(value = Common.PARAMS, required = true) String params,
			HttpServletRequest request) {
		return basicController.basic(params, BASE_SERVICE, request, RackController.class.getSimpleName());
	}

	/**
	 * 查看机柜的负载信息
	 * 
	 * @param params
	 *            机柜属性信息的json字符串，在数据转换时会发生异常，对外抛出400异常【Bad Request】
	 * @return 操作结果
	 */
	@RequestMapping(value = "/load", method = { RequestMethod.GET })
	public BsmResult load(@RequestParam(value = Common.PARAMS, required = true) String params,
			HttpServletRequest request) {
		JSONObject jsonObject = JSONTools.isJSONObj(params);
		if (null != jsonObject && jsonObject.containsKey(Common.ID)) {
			String url = UrlTranslator.translate(BASE_SERVICE + "/load", jsonObject.get(Common.ID));
			RemoteService service = serviceFactory.safeBuild(SERVICE, url, BoCloudMethod.LOAD, null, null, request);
			return service.invoke();
		} else {
			return ResultTools.formatErrResult();
		}
	}

	@RequestMapping(value = "/resources", method = { RequestMethod.GET })
	public BsmResult servers(@RequestParam(value = Common.PARAMS, required = true) String params,
			HttpServletRequest request) {
		JSONObject jsonObject = JSONTools.isJSONObj(params);
		if (null != jsonObject && jsonObject.containsKey(Common.ID)) {
			String url = UrlTranslator.translate(BASE_SERVICE + "/resources", jsonObject.get(Common.ID));
			RemoteService service = serviceFactory.safeBuild(SERVICE, url, BoCloudMethod.SUBOBJ, null, null, request);
			return service.invoke();
		} else {
			return ResultTools.formatErrResult();
		}
	}

	/**
	 * 查看机柜的高负载信息
	 * 
	 * @param params
	 *            机柜属性信息的json字符串，在数据转换时会发生异常，对外抛出400异常【Bad Request】
	 * @return 操作结果
	 */
	@RequestMapping(value = "/topLoad", method = { RequestMethod.GET })
	public BsmResult topLoad(@RequestParam(value = Common.PARAMS) String params, HttpServletRequest request) {
		JSONObject jsonObject = JSONTools.isJSONObj(params);
		if (null != jsonObject && jsonObject.containsKey(Common.ID)) {
			String url = UrlTranslator.translate(BASE_SERVICE + "/topLoad", jsonObject.get(Common.ID));
			RemoteService service = serviceFactory.safeBuild(SERVICE, url, BoCloudMethod.LOAD, null, null, request);
			return service.invoke();
		} else {
			return ResultTools.formatErrResult();
		}
	}

	/**
	 * 查看机柜的卡槽范围
	 * 
	 * @param params
	 *            机柜属性信息的json字符串，在数据转换时会发生异常，对外抛出400异常【Bad Request】
	 * @return 操作结果
	 */
	@RequestMapping(value = "/checkRange", method = { RequestMethod.GET })
	public BsmResult checkRange(@RequestParam(value = Common.PARAMS, required = true) String params,
			HttpServletRequest request) {
		JSONObject jsonObject = JSONTools.isJSONObj(params);
		if (null != jsonObject && jsonObject.containsKey(Common.ID)) {
			Map<String, Object> paramMap = MapTools.simpleMap(Common.SLOT, jsonObject.get(Common.SLOT));
			String url = UrlTranslator.translate(BASE_SERVICE + "/checkRange", jsonObject.get(Common.ID));
			RemoteService service = serviceFactory.safeBuild(SERVICE, url, BoCloudMethod.BASIC, null, paramMap,
					request);
			return service.invoke();
		} else {
			return ResultTools.formatErrResult();
		}
	}

	/**
	 * 查看机柜的卡槽位置是否被使用
	 * 
	 * @param params
	 *            机柜属性信息的json字符串，在数据转换时会发生异常，对外抛出400异常【Bad Request】
	 * @return 操作结果
	 */
	@RequestMapping(value = "/checkSlot", method = { RequestMethod.GET })
	public BsmResult checkSlot(@RequestParam(value = Common.PARAMS, required = true) String params,
			HttpServletRequest request) {
		JSONObject jsonObject = JSONTools.isJSONObj(params);
		if (null != jsonObject && jsonObject.containsKey(Common.ID)) {
			Map<String, Object> paramMap = MapTools.simpleMap(Common.SLOT_START, jsonObject.get(Common.SLOT_START));
			paramMap.put(Common.SLOT_NUM, jsonObject.get(Common.SLOT_NUM));
			if (null != jsonObject.get(Common.SERVER)) {
				paramMap.put(Common.SERVER, jsonObject.get(Common.SERVER));
			}
			String url = UrlTranslator.translate(BASE_SERVICE + "/checkSlot", jsonObject.get(Common.ID));
			RemoteService service = serviceFactory.safeBuild(SERVICE, url, BoCloudMethod.BASIC, null, paramMap,
					request);
			return service.invoke();
		} else {
			return ResultTools.formatErrResult();
		}
	}

	/**
	 * 得到机柜的卡槽使用情况
	 * 
	 * @param params
	 *            机柜属性信息的json字符串，在数据转换时会发生异常，对外抛出400异常【Bad Request】
	 * @return 操作结果
	 */
	@RequestMapping(value = "/slots", method = { RequestMethod.GET })
	public BsmResult slots(@RequestParam(value = Common.PARAMS, required = true) String params,
			HttpServletRequest request) {
		JSONObject jsonObject = JSONTools.isJSONObj(params);
		if (null != jsonObject && jsonObject.containsKey(Common.ID)) {
			String url = UrlTranslator.translate(BASE_SERVICE + "/slots", jsonObject.get(Common.ID));
			RemoteService service = serviceFactory.safeBuild(SERVICE, url, BoCloudMethod.SUBOBJ, null, null, request);
			return service.invoke();
		} else {
			return ResultTools.formatErrResult();
		}
	}

	/**
	 * 查看机柜的告警信息
	 * 
	 * @param params
	 *            机房属性信息的json字符串，在数据转换时会发生异常，对外抛出400异常【Bad Request】
	 * @return 操作结果
	 */
	@RequestMapping(value = "/alarms", method = { RequestMethod.GET })
	public BsmResult alarms(@RequestParam(value = Common.PARAMS, required = true) String params,
			HttpServletRequest request) {
		JSONObject jsonObject = JSONTools.isJSONObj(params);
		if (null != jsonObject && jsonObject.containsKey(Common.ID)) {
			String url = UrlTranslator.translate(BASE_SERVICE + "/alarms", jsonObject.get(Common.ID));
			RemoteService service = serviceFactory.safeBuild(SERVICE, url, BoCloudMethod.LOAD, null, null, request);
			return service.invoke();
		} else {
			return ResultTools.formatErrResult();
		}
	}

}
